package org.comet.auxiliary.client.swing.versions.v1;

import cn.liaobei.EasyLogin;
import lombok.SneakyThrows;


import org.comet.auxiliary.client.liaobei.wss.MsgHelper;
import org.comet.auxiliary.common.ClientReaderHandler;

import org.comet.auxiliary.common.handler.platform.PlatformHandler;
import org.comet.auxiliary.common.handler.soft.SoftHandler;
import org.comet.auxiliary.common.platform.HttpPlatformHandler;
import org.comet.auxiliary.common.swing.AbstractLoginSwing;
import org.comet.auxiliary.common.swing.LoginSwing;
import org.comet.auxiliary.common.swing.SettingSwing;
import org.comet.auxiliary.common.swing.SwingHelper;
import org.comet.auxiliary.common.util.MapBean;
import org.comet.auxiliary.common.util.config.ClientConfigHelper;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.UUID;

/**
 * @author wq
 * @date 2019/10/8
 * @description login client
 * 129.226.76.128
 */
public class LoginFrame extends AbstractLoginSwing implements LoginSwing, ActionListener {

    private PlatformHandler platformHandler = new HttpPlatformHandler();
//    private JTextField keyTextField;
//    private JButton keyButton;
//    private JButton cancelButton;
//    private JPanel loginPanel2;
//    private JPanel loginPanel;
//    private JPasswordField codeText;
//    private JTextField phoneText;

    public static void main(String[] args) {
//        ThreadUtil.initCommon(20, 200, 10, 20);
//        ThreadUtil.initSchedule(7);
//        ClientConfigHelper.setConfigPrefix("lapu.seconds.");
//        SwingHelper.setCustomFont(new Font("微软雅黑", Font.PLAIN, 13));
        initLogs();
//        LoginFrame loginFrame = new LoginFrame();
//        loginFrame.initAndLoadConfig();


    }

    private static void initLogs() {
        // platform: can be deleted
        SwingHelper.addLogsMap("SAVE_CONFIG_SUCCESS", "保存配置成功");
        SwingHelper.addLogsMap("JUMP_TO_RE_LOGIN_FAIL_TIP", "跳转重新登录界面失败: %s");
        SwingHelper.addLogsMap("QUERY_BALANCE_FAIL_TIP", "查询余额失败!");
        SwingHelper.addLogsMap("QUERY_BALANCE_ERROR_TIP", "查询余额失败: %s");


        SwingHelper.addLogsMap("START_RECONNECT_SERVER_TIP", "");
        SwingHelper.addLogsMap("CONNECT_SERVER_SUCCESS_TIP", "服务器连接成功!");
        SwingHelper.addLogsMap("CONNECT_SERVER_FAIL_TIP", "连接服务器失败, %s后重试...");
        SwingHelper.addLogsMap("CONNECT_SERVER_RETRY_TIP", "");
        SwingHelper.addLogsMap("CONNECT_SERVER_ERROR_TIP", "服务器连接异常：%s");
        SwingHelper.addLogsMap("SERVER_NOT_CONNECTED_TIP", "");

        // soft layer liaobei

        SwingHelper.addLogsMap("SAVE_CONFIG_SUCCESS", "保存配置成功");
        SwingHelper.addLogsMap("TOKEN_EXPIRE_TIP", "--------------身份登录信息过期, 重试重新登录!--------------");
        SwingHelper.addLogsMap("SEND_TM_ERROR_FRIEND_INFO_TIP", "好友信息未加载完或不存在, 导致发送消息失败! 请稍后重试");
        SwingHelper.addLogsMap("START_REFRESH_TOKEN_TIP", "刷新登录信息: %s");
        SwingHelper.addLogsMap("REFRESH_TOKEN_SUCCESS_TIP", "刷新登录信息成功: %s");
        SwingHelper.addLogsMap("REFRESH_TOKEN_FAIL_TIP", "刷新登录信息失败: %s");
        SwingHelper.addLogsMap("REFRESH_ADDRESS_BOOK_FAIL_TIP", "加载好友信息失败: %s");
        SwingHelper.addLogsMap("LIAOBEI_CHANNEL_HAS_EXCEPTION", "唯聊通讯异常: %s");
        SwingHelper.addLogsMap("LIAOBEI_CHANNEL_RESTORE_TIP", "主动断开与唯聊连接, 变更wssId!");


        SwingHelper.addLogsMap("LIAOBEI_CHANNEL_INACTIVE_TO_RETRY_TIP", "");//%s检测到唯聊连接被断开, 正在重连...
        SwingHelper.addLogsMap("CONNECT_LIAOBEI_SUCCESS_TIP", "");
        SwingHelper.addLogsMap("LIAOBEI_CHANNEL_SUCCESS_TIP", "");//唯聊通讯建立成功!
        SwingHelper.addLogsMap("CONNECT_LB_SERVER_FAIL_TIP", "");
        SwingHelper.addLogsMap("CONNECT_LIAOBEI_FAIL_TIP", "唯聊连接失败: %s");
        SwingHelper.addLogsMap("RECONNECT_LIAOBEI_FAIL_TIP", "唯聊重连失败: %s");
        SwingHelper.addLogsMap("LIAOBEI_CHANNEL_RETRY_TOO_MUCH", "");
        SwingHelper.addLogsMap("LIAOBEI_CHANNEL_LATER_RETRY_TIP", "");
        SwingHelper.addLogsMap("CONNECT_LIAOBEI_REPEAT_TIP", "");

        SwingHelper.addLogsMap("CONNECT_LB_SERVER_FAIL_TIP", "连接唯聊失败, %s后重试...");
        SwingHelper.addLogsMap("CONNECT_LB_SERVER_RETRY_TIP", "");

        // function layer
        SwingHelper.addLogsMap("TAKE_PRIVATE_PACKET_TIP", "领取了一个私聊红包，金额：%s，红包描述：%s");
        SwingHelper.addLogsMap("TAKE_PRIVATE_PACKET_ERROR_TIP", "领取私包时出错: %s");
        SwingHelper.addLogsMap("TAKE_RELATION_PACKET_TIP", "领取了一个红包，领取金额：%s");
        SwingHelper.addLogsMap("TAKE_RELATION_PACKET_ERROR_TIP", "领取包时出错: %s");

        SwingHelper.addLogsMap("WAIT_A_SECOND_BEFORE_CONNECT_WSS", "防止拥挤, 等待 %s 后继续...");


        SwingHelper.addLogsMap("CHOSE_SEND_OBJ", "选择抢包对象成功:");
        SwingHelper.addLogsMap("CHOSE_SEND_OBJ_SUCCESS", "选择抢包对象成功: %s - %s");
        SwingHelper.addLogsMap("START_SEND_TIP", "开始工作");
        SwingHelper.addLogsMap("STOP_SEND_TIP", "已停止工作");
        SwingHelper.addLogsMap("LOGIN_SUCCESS_TIP", "登录成功: 账号: %s");
        SwingHelper.addLogsMap("LOGIN_FAIL_TIP", "登录失败: 账号或密码不对! 账号: %s");
        SwingHelper.addLogsMap("IMPORT_LOGOUT_TIP", "您退出了重要账号: %s, 将导致所有账号无法领取红包, 请立刻登录任意一个号!");
        SwingHelper.addLogsMap("IMPORT_LOGIN_TIP", "账号: %s被选定为重要账号, 请不要随意退出此账号!");
        SwingHelper.addLogsMap("SEND_PAG_ERROR", "账号: %s发送红包失败，原因:%s");
        SwingHelper.addLogsMap("SEND_PAG_SUCCESS", "账号: %s发送红包成功");
    }

    public static void saveKey(String key, String overdueTime) {
        MapBean config = new MapBean();
        config.put("key", key);
        config.put("overdue", overdueTime);
        ClientConfigHelper.saveConfig(config);
    }

    @Override
    protected void doInit() {
        UUID uuid= UUID.randomUUID();
        try {
            EasyLogin.login(uuid.toString(),new MsgHelper());
            loginSuccess();
        } catch (Exception e) {
            e.printStackTrace();
        }
//        //获取当前窗口的内容窗格
//        loginPanel = new JPanel();
//        loginPanel.setVisible(false);
//        loginPanel.setBorder(BorderFactory.createEmptyBorder(2, 10, 5, 1));
//        loginPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
//
//        JLabel validKeyLabel = new JLabel(getKeyLabelText() + "：");
//        keyTextField = new JTextField(12);
//        keyButton = new JButton(getLoginText());
//        keyButton.setActionCommand("login");
//        keyButton.addActionListener(this);
//
//        cancelButton = new JButton(getCancelText());
//        cancelButton.setActionCommand("cancel");
//        cancelButton.addActionListener(this);
//        cancelButton.setEnabled(false);
//
//        loginPanel.add(validKeyLabel);
//        loginPanel.add(keyTextField);
//        loginPanel.add(keyButton);
//        loginPanel.add(cancelButton);
//
//        loginPanel2 = new JPanel();
//        loginPanel2.setVisible(false);
//        loginPanel2.setBorder(BorderFactory.createEmptyBorder(10, 20, 0, 20));
//        loginPanel2.setLayout(new GridLayout(3, 2, 2, 0));
//
//        JLabel phoneLabel = new JLabel("手机号: ");
//        phoneText = new JTextField(15);
//        JLabel codeLabel = new JLabel("密  码: ");
//        codeText = new JPasswordField(10);
//
//        JButton login2Btn = new JButton("登录");
//        login2Btn.setActionCommand("codelogin");
//        login2Btn.addActionListener(this);
//
//
//        loginPanel2.add(phoneLabel);
//        loginPanel2.add(phoneText);
//        loginPanel2.add(codeLabel);
//        loginPanel2.add(codeText);
//        loginPanel2.add(login2Btn);
//
//        setLayout(new FlowLayout(FlowLayout.LEFT, 1, 1));
//        Container container = getContentPane();
//        container.add(loginPanel);
//        container.add(loginPanel2);
//
//        afterInit();
    }

    protected void afterInit() {
    }

    protected String getCancelText() {
        return "取消";
    }

    @Override
    protected void renderQRCode(URL qrUrl) {
//        MapBean config = ClientConfigHelper.getConfig();
//        String phone = config.getString("login");
//        if (!StringUtil.isBlank(phone)) {
//            phoneText.setText(phone);
//            codeText.requestFocus();
//        }
    }

    @Override
    protected void showKey() {
//        setTitle(getLoginSwingTitle());
//        setSize(getLoginFrameWidth(), getKeyModeHeight());
//        loginPanel.setVisible(true);
//        loginPanel2.setVisible(false);
    }

    @Override
    protected void hideKey() {
//        loginPanel.setVisible(false);
//        loginPanel2.setVisible(false);
//        try {
//            loginSuccess();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }

    @Override
    protected void connect() {
        this.hasConnect = true;
        validKey();
    }

    @SneakyThrows
    @Override
    protected void validKey() {
//        MapBean config = ClientConfigHelper.getConfig();
//        String host = config.getString("ip");
//        Integer port = config.getInteger("port");
//        Object ip = ClientConfigHelper.getOtherConfig("updater.server.ip");
//        Object port0 = ClientConfigHelper.getOtherConfig("updater.server.port");
//        Object url = ClientConfigHelper.getOtherConfig("updater.server.url");
//        String url0 = "/";
//        if (url != null) {
//            url0 = url.toString();
//        }
//        if (ip != null) {
//            host = ip.toString();
//        }
//
//        if (port0 != null) {
//            port = Integer.parseInt(port0.toString());
//        }
//        String keyText = this.getInputKey();
//        Map<String, Object> res = LapuUtil.validKey(host, getProduct(), port, url0, keyText);
//        int res1 = Integer.parseInt(res.get("res").toString());
//        if (res1 == 0) {
//            String overdue = res.get("overdue").toString();
//            saveKey(keyText, overdue);
//            loginSuccess();
//        } else {
//            String errMsg = "卡密不存在";
//            if (res1 == 2) {
//                errMsg = "卡密已封停";
//            } else if (res1 == 3) {
//                errMsg = "卡密已过期";
//            } else if (res1 == 5) {
//                errMsg = "卡密已绑定其他机器";
//            }
//
//            SwingHelper.loginFail(errMsg);
//        }
    }
    //账号
    public static  String account=null;
    public static String pwd=null;
    @SneakyThrows
    @Override
    public void actionPerformed(ActionEvent event) {
//        String actionCommand = event.getActionCommand();
//
//        if ("codelogin".equals(actionCommand)) {
//            account= phoneText.getText();
//            pwd= codeText.getText();
//            new LapuSoftHandler().loginByAccount(account, pwd, true, () -> {
//                System.out.println("登录成功");
//                SwingHelper.addLog("IMPORT_LOGIN_TIP", account);
//                try{
//                    loginSuccess();
//                }catch (Exception e){
//                    System.out.println("报错");
//                }
//
//            }, () -> {
//                //error
//                System.out.println("登录失败");
////                accountInfo.put("status", "登录失败");
//                SwingHelper.addLog("LOGIN_FAIL_TIP", account, pwd);
//            });
//
//
//
//
//        }
    }

    @Override
    protected void loginLoading() {
//        cancelButton.setEnabled(true);
//        keyButton.setText(getLoadingText());
//        keyButton.setActionCommand("loading");
//        keyButton.setEnabled(false);
//        keyTextField.setEnabled(false);
    }


    @Override
    protected void loginLoaded() {
//        cancelButton.setEnabled(false);
//        keyButton.setText(getLoginText());
//        keyButton.setActionCommand("login");
//        keyButton.setEnabled(true);
//        keyTextField.setEnabled(true);
//        keyTextField.requestFocus();
//        keyTextField.selectAll();
    }

    @Override
    protected String getInputKey() {
        return null;
    }

    @Override
    protected void renderKey(String key) {

    }

    @Override
    protected String getProduct() {
        return "seconds";
    }

    @Override
    public SoftHandler getSoftHandler() {
        return null;
    }

    @Override
    protected PlatformHandler getPlatformHandler() {
        return platformHandler;
    }

    @Override
    protected ClientReaderHandler getReaderHandler() {
        return null;
    }

    @Override
    protected SettingSwing newSettingSwing() {
        return new SettingFrame();
    }

    protected String getLoginText() {
        return "登录";
    }

    protected String getLoadingText() {
        return "登录中..";
    }

    protected String getKeyLabelText() {
        return "卡密";
    }

    @Override
    protected int getLoginFrameWidth() {
        return 450;
    }

    @Override
    protected int getKeyModeHeight() {
        return 150;
    }

    @Override
    protected int getQRModeHeight() {
        return 153;
    }





}